package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.repositoryService.NotifyMessageRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Account;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import com.ktwlsoft.archivemanage.service.security.UserPrincipal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(description = "消息管理")
@RestController
@RequestMapping("/messageController")
public class MessageController {

    private final NotifyMessageRepositoryService notifyMessageRepositoryService;

    public MessageController(NotifyMessageRepositoryService notifyMessageRepositoryService) {
        this.notifyMessageRepositoryService = notifyMessageRepositoryService;
    }

    @ApiOperation("查找所有消息")
    @GetMapping("/findByPage")
    @UserAuthenticate(actionCode = "MESSAGE_LIST", skip = true)
    public ResponseEntity<?> findByPage(@RequestParam Integer pn, @RequestParam Integer pageSize, Long recorderId, @ApiParam("客户端传5, 管理端传0") @RequestParam Long export) {
        Account account = UserPrincipal.getCurrentUser();
        if(account == null){
            return ResponseEntity.ok(Result.body(null, ResultType.CHECK_ERROR).withMessage("获取不到当前用户"));
        }
        return ResponseEntity.ok(notifyMessageRepositoryService.findByPage(pn, pageSize, recorderId, export,account));
    }

    @ApiOperation("设置已读")
    @GetMapping("/setRead")
    public ResponseEntity<Result> setRead(@RequestParam Long id){
        notifyMessageRepositoryService.setMsgRead(id);
        return ResponseEntity.ok(Result.ok("操作完成"));
    }
}
